package com.aydqyy.springbootstart.config;

import com.aydqyy.springbootstart.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    private LoginInterceptor   loginInterceptor;
    @Autowired
    public WebConfig(LoginInterceptor loginInterceptor) {
        this.loginInterceptor = loginInterceptor;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/register","/user/login");
    }
    //全局配置跨域访问Preflight请求响应
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173") // 指定前端域名
                .allowedMethods("GET", "POST", "OPTIONS") // 包含 OPTIONS
                .allowedHeaders("Authorization", "Content-Type")
                .allowCredentials(true) // 允许携带凭证（如 Cookie）
                .maxAge(3600);
    }
}

